package com.fufufu.mail.product.service.impl;

import com.fufufu.mail.product.vo.Images;
import com.fufufu.mail.product.vo.Skus;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fufufu.common.utils.PageUtils;
import com.fufufu.common.utils.Query;

import com.fufufu.mail.product.dao.SkuInfoDao;
import com.fufufu.mail.product.entity.SkuInfoEntity;
import com.fufufu.mail.product.service.SkuInfoService;
import org.springframework.util.StringUtils;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Override
    public Map<Long, Skus> saveSpuInfo(Long catalogId, Long brandId, Long spuId, List<Skus> skus) {
        Map<Long, Skus> mapping = new HashMap<>(skus.size());
        Map<SkuInfoEntity, Skus> map = new HashMap<>(skus.size());
        List<SkuInfoEntity> skuInfoEntities = skus.stream().map((sku) -> {
            SkuInfoEntity skuInfoEntity = new SkuInfoEntity();
            skuInfoEntity.setBrandId(brandId);
            skuInfoEntity.setCatalogId(catalogId);
            skuInfoEntity.setSkuName(sku.getSkuName());
            skuInfoEntity.setSpuId(spuId);
            skuInfoEntity.setPrice(sku.getPrice());
            skuInfoEntity.setSkuTitle(sku.getSkuTitle());
            skuInfoEntity.setSkuSubtitle(sku.getSkuSubtitle());
            skuInfoEntity.setSaleCount(0L);
            for (Images image : sku.getImages()) {
                if (image.getDefaultImg() == 1) {
                    skuInfoEntity.setSkuDefaultImg(image.getImgUrl());
                }
            }
            map.put(skuInfoEntity, sku);
            return skuInfoEntity;
        }).collect(Collectors.toList());
        this.saveBatch(skuInfoEntities);
        for (SkuInfoEntity skuInfoEntity : skuInfoEntities) {
            mapping.put(skuInfoEntity.getSkuId(), map.get(skuInfoEntity));
        }
        return mapping;
    }
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<SkuInfoEntity> queryWrapper = new QueryWrapper<>();

        String catelogId = (String) params.get("catelogId");
        if (StringUtils.hasText(catelogId) && !"0".equals(catelogId)) {
            queryWrapper.eq("catalog_id", catelogId);
        }
        String brandId = (String) params.get("brandId");
        if (StringUtils.hasText(brandId) && !"0".equals(brandId)) {
            queryWrapper.eq("brand_id", brandId);
        }
        String min = (String) params.get("min");
        if (StringUtils.hasText(min) && !"0".equals(min)) {
            // 大于等于
            queryWrapper.ge("price", min);

        }
        String max = (String) params.get("max");
        if (StringUtils.hasText(max) && !"0".equals(max)) {
            // 小于等于
            queryWrapper.le("price", max);
        }
        String key = (String) params.get("key");
        if (StringUtils.hasText(key)) {
            queryWrapper.and((wrapper) -> {
                wrapper.eq("sku_id", key).or().like("sku_name", key);
            });
        }

        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                queryWrapper
        );
        return new PageUtils(page);
    }

}